package com.zat.mes.andrmes.dofun.work.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.base.BaseFragment
import com.zat.mes.andrmes.dofun.work.DayWorkRecordActivity
import com.zat.mes.andrmes.dofun.work.DayWorkRecordListActivity
import com.zat.mes.andrmes.dofun.work.adapter.DayRecordPlanAdapter
import com.zat.mes.andrmes.dofun.work.vm.WorkRecordViewModel
import com.zat.mes.andrmes.event.WorkRecordEventMessage
import kotlinx.android.synthetic.main.fragment_day_produce_plan.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author zy
 * @date :2020-02-25 14:05
 * @description: 日生产计划记录fragment
 */
class DayRecordFragment : BaseFragment() {


    private var dayRecordAdapter: DayRecordPlanAdapter? = null
    private var workModel: WorkRecordViewModel? = null
    private var empId: Long = 0
    private var dayActivity: DayWorkRecordActivity? = null
    private var dayListActivity: DayWorkRecordListActivity? = null

    companion object {
        private val EMPID = "empId"
        fun newInstance(empId: Long): DayRecordFragment {
            val fragment = DayRecordFragment()
            val args = Bundle()
            args.putLong(EMPID, empId)
            fragment.arguments = args
            return fragment
        }
    }

    override fun getViewId(): Int {
        return R.layout.fragment_day_produce_plan
    }
    /**
     * 是否开启titlebar
     */
    override fun isOpenTitleBar(): Boolean {
        return false
    }

    override fun initView() {
    }

    /**
     * 初始化数据
     */
    override fun initData() {
        EventBus.getDefault().register(this);
        var activity: BaseActivity = context as BaseActivity
        if (activity is DayWorkRecordActivity) {
            dayActivity = activity
        } else if (activity is DayWorkRecordListActivity) {
            dayListActivity = activity
        }
        if (arguments?.getLong(EMPID) != null) {
            empId = arguments?.getLong(EMPID)!!

        }
        recylerDayProducePlan.layoutManager = LinearLayoutManager(context)
        dayRecordAdapter =
            DayRecordPlanAdapter(R.layout.item_fragment_day_produce_plan)
        recylerDayProducePlan.adapter = dayRecordAdapter

        loadModel()
    }

    /**
     * 初始化model
     */
    private fun loadModel() {
        workModel = ViewModelProvider(this).get(WorkRecordViewModel::class.java)
        var date = ""
        if (dayActivity != null) {
            date = dayActivity?.selectedDate.toString()
        } else if (dayListActivity != null) {
            date = dayListActivity?.day.toString()
        }
        workModel?.getWorkRecordData()?.observe(this, Observer {
            closeLoading()
            if (it != null && it.size > 0) {
                setEmptyGone()
            } else {
                setEmptyVisible()
            }
            dayRecordAdapter?.setNewData(it)
        })
        workModel?.loadDayRecordList(empId, date)
        showLoading()
    }

    /**
     * 刷新数据监听
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onDateChangeEvent(event: WorkRecordEventMessage?) {
        if (workModel != null) {
            var date = ""
            if (dayActivity != null) {
                date = dayActivity?.selectedDate.toString()
            } else if (dayListActivity != null) {
                date = dayListActivity?.day.toString()
            }
            workModel?.loadDayRecordList(empId, date)
            showLoading()
        }
    }

    override fun onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView()
    }
}